<?php

namespace App\Http\Controllers\Admin;

use App\Exports\FangOwnerExport;
use App\Models\FangOwner;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class FangOwnerController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = FangOwner::paginate($this->pagesize);
        return view('admin.fangowner.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.fangowner.create');
    }


    public function upfile(Request $request)
    {
        //默认图标
        $pic = config('up.pic');
        if($request->hasFile('file')){
            //参数2 配置的节点名称
            $ret = $request->file('file')->store('','fangowner');
            $pic = '/uploads/fangowner/'.$ret;
        }
        return ['status'=>0,'url'=>$pic];
    }

    public function delfile(Request $request)
    {
        $filepath = $request->get('file');
        //得到真实地址
        $path = public_path().$filepath;
        //删除指定文件
        unlink($path);
        return ['status'=>0,'msg'=>'成功'];
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $this->validate($request,[
            'name'=>'required'
        ]);
        $postData = $request->except(['_token','file']);
        $postData['pic'] = trim($postData['pic'],'#'); //去除#

        FangOwner::create($postData);
        return redirect(route('admin.fangowner.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\FangOwner  $fangOwner
     * @return \Illuminate\Http\Response
     */
    public function show(FangOwner $fangowner)
    {
        //
        $picList = explode('#',$fangowner->pic);
        //遍历
        array_map(function ($item){
            echo "<img src='$item' style=width:100px />";
        },$picList);
    }

    //导出
    public function exports()
    {
        return Excel::download(new FangOwnerExport(),'fangowner.xlsx');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\FangOwner  $fangOwner
     * @return \Illuminate\Http\Response
     */
    public function edit(FangOwner $fangowner)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\FangOwner  $fangOwner
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, FangOwner $fangowner)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\FangOwner  $fangOwner
     * @return \Illuminate\Http\Response
     */
    public function destroy(FangOwner $fangowner)
    {
        //
    }
}
